<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="前言最近想想自己仿写vant，搞清楚每个组件的实现方法，所以就想自己建立一个vue的组件库，并将其放到npm上。下面就讲解如何使用vue-cli创建自己的组件库并打包发布到npm上 安装vue-cli并初始化一个vue项目12345npm install -g @vue/cli# oryarn global add @vue/clivue create vant-copycd vant-copy">
<meta name="keywords" content="有赞">
<meta property="og:type" content="article">
<meta property="og:title" content="如何使用vue-cli创建自己的vue组件库">
<meta property="og:url" content="https://andyliwr.github.io/2019/07/22/vue_component_library/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="前言最近想想自己仿写vant，搞清楚每个组件的实现方法，所以就想自己建立一个vue的组件库，并将其放到npm上。下面就讲解如何使用vue-cli创建自己的组件库并打包发布到npm上 安装vue-cli并初始化一个vue项目12345npm install -g @vue/cli# oryarn global add @vue/clivue create vant-copycd vant-copy">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://img.vim-cn.com/59/d7f8814d0f67906eee5ab7c3e0a2ee00bb2f26.jpg">
<meta property="og:updated_time" content="2019-07-22T17:40:47.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="如何使用vue-cli创建自己的vue组件库">
<meta name="twitter:description" content="前言最近想想自己仿写vant，搞清楚每个组件的实现方法，所以就想自己建立一个vue的组件库，并将其放到npm上。下面就讲解如何使用vue-cli创建自己的组件库并打包发布到npm上 安装vue-cli并初始化一个vue项目12345npm install -g @vue/cli# oryarn global add @vue/clivue create vant-copycd vant-copy">
<meta name="twitter:image" content="https://img.vim-cn.com/59/d7f8814d0f67906eee5ab7c3e0a2ee00bb2f26.jpg">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>如何使用vue-cli创建自己的vue组件库</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/07/25/zan_proxy/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/07/22/youzan_working/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2019/07/22/vue_component_library/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&text=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&is_video=false&description=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=如何使用vue-cli创建自己的vue组件库&body=Check out this article: https://andyliwr.github.io/2019/07/22/vue_component_library/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&name=如何使用vue-cli创建自己的vue组件库&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装vue-cli并初始化一个vue项目"><span class="toc-number">2.</span> <span class="toc-text">安装vue-cli并初始化一个vue项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改造目录"><span class="toc-number">3.</span> <span class="toc-text">改造目录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编写一个Button组件"><span class="toc-number">4.</span> <span class="toc-text">编写一个Button组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用vue-cli-service打包组件"><span class="toc-number">5.</span> <span class="toc-text">使用vue-cli-service打包组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#上传到npm"><span class="toc-number">6.</span> <span class="toc-text">上传到npm</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">7.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        如何使用vue-cli创建自己的vue组件库
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2019-07-22T17:40:47.000Z" itemprop="datePublished">2019-07-22</time>
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/有赞/">有赞</a>
    </div>


    </div>
  </header>
  
<div class="article-gallery">
    
    <a class="gallery-item" href="https://img.vim-cn.com/59/d7f8814d0f67906eee5ab7c3e0a2ee00bb2f26.jpg" rel="gallery_ck5exevsb0040ayoj3aunh6mj">
        <img src="https://img.vim-cn.com/59/d7f8814d0f67906eee5ab7c3e0a2ee00bb2f26.jpg" itemprop="image" />
    </a>
    
</div>


  <div class="content" itemprop="articleBody">
    <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>最近想想自己仿写<code>vant</code>，搞清楚每个组件的实现方法，所以就想自己建立一个vue的组件库，并将其放到npm上。下面就讲解如何使用vue-cli创建自己的组件库并打包发布到npm上</p>
<h3 id="安装vue-cli并初始化一个vue项目"><a href="#安装vue-cli并初始化一个vue项目" class="headerlink" title="安装vue-cli并初始化一个vue项目"></a>安装vue-cli并初始化一个vue项目</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">npm install -g @vue/cli</span><br><span class="line"><span class="comment"># or</span></span><br><span class="line">yarn global add @vue/cli</span><br><span class="line">vue create vant-copy</span><br><span class="line"><span class="built_in">cd</span> vant-copy &amp;&amp; npm run serve</span><br></pre></td></tr></table></figure>
<h3 id="改造目录"><a href="#改造目录" class="headerlink" title="改造目录"></a>改造目录</h3><ol>
<li>将<code>src</code>目录重命名为<code>examples</code>目录用来放置测试组件的代码</li>
<li>新建一个<code>packages</code>目录用来放置组件代码</li>
<li>新建vue.config.js来重写vue开发环境的配置<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vue配置文件</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="comment">// 将 examples 目录添加为新的页面</span></span><br><span class="line">  pages: &#123;</span><br><span class="line">    index: &#123;</span><br><span class="line">      <span class="comment">// page 的入口</span></span><br><span class="line">      entry: <span class="string">'examples/main.js'</span>,</span><br><span class="line">      <span class="comment">// 模板来源</span></span><br><span class="line">      template: <span class="string">'public/index.html'</span>,</span><br><span class="line">      <span class="comment">// 输出文件名</span></span><br><span class="line">      filename: <span class="string">'index.html'</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><img src="https://file.lantingshucheng.com/2019-07-23-18-46-07.png?imageView2/1/w/200/h/200" alt="2019-07-23-18-46-07"></p>
<h3 id="编写一个Button组件"><a href="#编写一个Button组件" class="headerlink" title="编写一个Button组件"></a>编写一个Button组件</h3><p>新建组件的vue文件<code>./packages/button/button.vue</code><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;button <span class="class"><span class="keyword">class</span></span>=<span class="string">"vant-btn"</span>&gt;</span><br><span class="line">    &lt;slot&gt;<span class="xml"><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span></span><br><span class="line">  &lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">'Button'</span></span><br><span class="line">&#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>安装<code>sass</code>，新建组件的样式文件<code>./packages/button/button.scss</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm install -D sass-loader node-sass</span><br></pre></td></tr></table></figure></p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.vant-btn</span> &#123;</span><br><span class="line">  <span class="attribute">border</span>: none;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">5px</span> <span class="number">15px</span>;</span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#41b883</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#ffffff</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>新建组件的总入口文件<code>./packages/index.js</code><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span>;</span><br><span class="line"><span class="keyword">import</span> Button <span class="keyword">from</span> <span class="string">'./button/button'</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">'./index.scss'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Components = &#123;</span><br><span class="line">  Button</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.keys(Components).forEach(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">  Vue.component(Components[key].name, Components[key])</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Components</span><br></pre></td></tr></table></figure></p>
<p>新建所有组件的scss文件<code>./src/components/index.scss</code>并导入刚才新建的按钮样式文件<br><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@import</span> <span class="string">'./button/button.scss'</span>;</span><br></pre></td></tr></table></figure></p>
<p>在<code>App.vue</code>中导入创建的组件库并预览新建组件的效果</p>
<p><code>./examples/main.js</code><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">'./App.vue'</span></span><br><span class="line"><span class="keyword">import</span> vant <span class="keyword">from</span> <span class="string">'../packages'</span></span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line">Vue.use(vant)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">&#125;).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure></p>
<p><code>./src/App.vue</code><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=<span class="string">"app"</span>&gt;</span><br><span class="line">    &lt;Button&gt;asda&lt;<span class="regexp">/Button&gt;</span></span><br><span class="line"><span class="regexp">  &lt;/</span>div&gt;</span><br><span class="line">&lt;<span class="regexp">/template&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;script&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">export default &#123;</span></span><br><span class="line"><span class="regexp">  name: 'app',</span></span><br><span class="line"><span class="regexp">  components: &#123;&#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp">&lt;/</span>script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">#app &#123;</span><br><span class="line">  font-family: <span class="string">'Avenir'</span>, Helvetica, Arial, sans-serif;</span><br><span class="line">  -webkit-font-smoothing: antialiased;</span><br><span class="line">  -moz-osx-font-smoothing: grayscale;</span><br><span class="line">  text-align: center;</span><br><span class="line">  color: #2c3e50;</span><br><span class="line">  margin-top: <span class="number">60</span>px;</span><br><span class="line">&#125;</span><br><span class="line">&lt;<span class="regexp">/style&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>效果应该就像下面这：</p>
<p><img src="https://file.lantingshucheng.com/2019-07-22-17-58-06.png" alt="2019-07-22-17-58-06"></p>
<h3 id="使用vue-cli-service打包组件"><a href="#使用vue-cli-service打包组件" class="headerlink" title="使用vue-cli-service打包组件"></a>使用<code>vue-cli-service</code>打包组件</h3><p>vue-cli提供vue-cli-service的命令行工具可以对vue组件进行打包，使用方式如下：<br><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">用法：vue-cli-service build [options] [entry|pattern]</span><br><span class="line"></span><br><span class="line">选项：</span><br><span class="line"></span><br><span class="line">  --mode        指定环境模式 (默认值：production)</span><br><span class="line">  --dest        指定输出目录 (默认值：dist)</span><br><span class="line">  --modern      面向现代浏览器带自动回退地构建应用</span><br><span class="line">  --target      app | <span class="class"><span class="keyword">lib</span> | <span class="title">wc</span> | <span class="title">wc</span>-<span class="title">async</span> (默认值：<span class="title">app</span>)</span></span><br><span class="line">  --name        库或 Web Components 模式下的名字 (默认值：package.json 中的 <span class="string">"name"</span> 字段或入口文件名)</span><br><span class="line">  --no-clean    在构建项目之前不清除目标目录</span><br><span class="line">  --report      生成 report.html 以帮助分析包内容</span><br><span class="line">  --report-json 生成 report.json 以帮助分析包内容</span><br><span class="line">  --watch       监听文件变化</span><br></pre></td></tr></table></figure></p>
<p>我们可以在<code>package.json</code>新增一个<code>script</code>，并修改<code>main</code>属性，将其指向到打包后生成的：<code>vant.common.js</code><br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">"main": "./lib/vant.common.js",</span><br><span class="line">"license": "MIT",</span><br><span class="line">"scripts": &#123;</span><br><span class="line">  ...</span><br><span class="line">  "build-component": "vue-cli-service build --target lib --name vant --dest lib ./packages/index.js"</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></p>
<p>最后运行的npm run build-component，生成的文件就像这样：<br><img src="https://file.lantingshucheng.com/2019-07-23-11-25-49.png" alt="2019-07-23-11-25-49"></p>
<h3 id="上传到npm"><a href="#上传到npm" class="headerlink" title="上传到npm"></a>上传到npm</h3><ol>
<li>修改<code>package.json</code>的name属性来定义npm包的名称，这里叫做<code>@andyliwr/vant-copy</code></li>
<li><p>然后定义<code>file</code>属性来选择上传哪些文件到npm上，需要注意的是npm有自己的忽略和必须上传文件的要求，所以有些文件可以不用写，npm也会自动上传，具体可以看<a href="https://docs.npmjs.com/files/package.json#files" target="_blank" rel="noopener">这里</a></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">"files": [</span><br><span class="line">  <span class="string">"lib"</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行上传</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># npm登录</span></span><br><span class="line">npm login</span><br><span class="line"><span class="comment"># 确认账号</span></span><br><span class="line">npm whoami</span><br><span class="line"><span class="comment"># 发布</span></span><br><span class="line">npm publish</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>最后打开<a href="https://www.npmjs.com/" target="_blank" rel="noopener">npm官网</a>，就可以看到上传的npm包了</p>
<p><img src="https://file.lantingshucheng.com/2019-07-23-11-31-46.png" alt="2019-07-23-11-31-46"></p>
<h3 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h3><p>到这里一个简单的组件库就做好了，后续会再加入自动化测试，和使用travis自动化发布npm，如果你有更好的建议或者困惑的地方，都可以发送邮件到我的邮箱 - <a href="andyliwr@outlook.com">andyliwr@outlook.com</a></p>

  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装vue-cli并初始化一个vue项目"><span class="toc-number">2.</span> <span class="toc-text">安装vue-cli并初始化一个vue项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改造目录"><span class="toc-number">3.</span> <span class="toc-text">改造目录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编写一个Button组件"><span class="toc-number">4.</span> <span class="toc-text">编写一个Button组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用vue-cli-service打包组件"><span class="toc-number">5.</span> <span class="toc-text">使用vue-cli-service打包组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#上传到npm"><span class="toc-number">6.</span> <span class="toc-text">上传到npm</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">7.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2019/07/22/vue_component_library/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&text=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&is_video=false&description=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=如何使用vue-cli创建自己的vue组件库&body=Check out this article: https://andyliwr.github.io/2019/07/22/vue_component_library/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&title=如何使用vue-cli创建自己的vue组件库"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2019/07/22/vue_component_library/&name=如何使用vue-cli创建自己的vue组件库&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


